import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Center from '@/views/Center'
import Films from '@/views/Films'
import Cinemas from '@/views/Cinemas'
import City from '@/views/City'
// import Search from '@/views/Search'
import Nowplaying from '@/views/films/Nowplaying'
import Detial from '@/views/Detial'
import Comingsoon from '@/views/films/Comingsoon'
// import Order from '@/views/Order'
import Login from '@/views/Login'

Vue.use(VueRouter)

const routes = [
  {
    path: '/films',
    // name: 'films  命名路由
    component: Films,
    children: [
      {
        path: '/films/nowplaying',
        component: Nowplaying
      },
      {
        path: '/films/comingsoon',
        component: Comingsoon
      },
      {
        path: '/films',
        redirect: '/films/nowplaying'
      }
    ]
  },
  {
    path: '/detial/:id',
    component: Detial
  },
  {
    path: '/cinemas',
    component: Cinemas
  },
  {
    path: '/city',
    component: City
  },
  {
    path: '/cinemas/search',
    component: () => import('@/views/Search')
  },
  {
    path: '/center',
    component: Center,
    meta: {
      isSwq: true
    }
  },
  {
    path: '/order',
    component: () => import('@/views/Order'),
    meta: {
      isSwq: true
    }
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '*',
    redirect: '/films'
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  // next()
  // console.log(to)
  // 先判断被拦截的路径
  if (to.meta.isSwq) {
    // 在判断拦截的路径满足要求
    if (localStorage.getItem('token')) {
      next()
    } else {
      //  next('/login')
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

export default router
